package org.fg.logcommon.config;

import ch.qos.logback.classic.Level;
import ch.qos.logback.classic.spi.ILoggingEvent;
import ch.qos.logback.core.filter.AbstractMatcherFilter;
import ch.qos.logback.core.spi.FilterReply;

public class DefaultDiscriminatorFilter extends AbstractMatcherFilter<ILoggingEvent> {

    private String defaultValue;

    private String key;

    @Override
    public void start() {
        //初始化
        onMismatch = FilterReply.DENY;
        super.start();
    }

    @Override
    public FilterReply decide(ILoggingEvent event) {
        if (Level.DEBUG.isGreaterOrEqual(event.getLevel())) {
            return onMismatch;
        }
        if (event.getMDCPropertyMap().containsKey(key)) {
            String value = event.getMDCPropertyMap().get(key);
            if (!value.equals(defaultValue)) {
                return onMatch;
            } else {
                return onMismatch;
            }
        } else {
            return onMismatch;
        }
    }

    public void setDefaultValue(String defaultValue) {
        this.defaultValue = defaultValue;
    }

    public void setKey(String key) {
        this.key = key;
    }
}
